package org.example.day10;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.3.1: 使用Condition类实现等待/通知
 *      注意: 执行condition.await();时必须先通过lock.lock()获取同步监视器(锁),否则报IllegalMonitorStateException异常
 */
public class MyThread7 {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    public void await(){
        try {
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyThread7 m7 = new MyThread7();
        Thread7A ta = new Thread7A(m7);
        ta.start();
    }
}

class Thread7A extends Thread{
    private MyThread7 m7;

    public Thread7A(MyThread7 m7) {
        this.m7 = m7;
    }

    @Override
    public void run() {
        m7.await();
    }
}